﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Column which outputs user-defined templates.
	/// </summary>
	public class TemplateColumn : BaseGridColumn
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public TemplateColumn()
		{
		}
		#endregion
		#region PROPS
		/// <summary>
		/// The template for the header of this column.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[TemplateInstance(TemplateInstance.Single)]
		public ITemplate THeader { get; set; }
		/// <summary>
		/// The container for the header template of this column.
		/// </summary>
		public Control THeaderContainer { get { return _headerContainer; } }
		/// <summary>
		/// The template for the view mode cell of this column.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate View { get; set; }
		/// <summary>
		/// The container for the view mode cell template of this column.
		/// </summary>
		public Control ViewContainer { get { return _viewContainer; } }
		/// <summary>
		/// The template for the edit mode cell of this column.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate Edit { get; set; }
		/// <summary>
		/// The container for the edit mode cell template of this column.
		/// </summary>
		public Control EditContainer { get { return _editContainer; } }
		/// <summary>
		/// The template for the footer of this column.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[TemplateInstance(TemplateInstance.Single)]
		public ITemplate TFooter { get; set; }
		/// <summary>
		/// The container for the footer template of this column.
		/// </summary>
		public Control TFooterContainer { get { return _footerContainer; } }
		#endregion
		#region METHODS
		/// <summary>
		/// Creates the header controls from the <see cref="THeader"/> template.
		/// </summary>
		/// <param name="container"></param>
		public override void CreateHeader(Control container, Grid owner)
		{
			if (THeader != null)
			{
				THeader.InstantiateIn(THeaderContainer);
			}
			if (THeaderContainer.Controls.Count > 0)
				container.Controls.Add(THeaderContainer);
			else
				base.CreateHeader(container, owner);
		}
		/// <summary>
		/// Creates the footer controls from the <see cref="TFooter"/> template.
		/// </summary>
		/// <param name="container"></param>
		public override void CreateFooter(Control container, Grid owner)
		{
			if (TFooter != null)
			{
				TFooter.InstantiateIn(TFooterContainer);
			}
			if (TFooterContainer.Controls.Count > 0)
				container.Controls.Add(TFooterContainer);
			else
				base.CreateFooter(container, owner);
		}
		/// <summary>
		/// Creates the view mode controls from the <see cref="View"/> template.
		/// </summary>
		/// <param name="container"></param>
		public override void CreateViewItem(Control container, Grid owner)
		{
			if (View != null)
			{
				View.InstantiateIn(ViewContainer);
			}
			container.Controls.Add(ViewContainer);
		}
		/// <summary>
		/// Creates the edit mode controls from the <see cref="Edit"/> template.
		/// </summary>
		/// <param name="container"></param>
		public override void CreateEditItem(Control container, Grid owner)
		{
			if (Edit != null)
			{
				Edit.InstantiateIn(EditContainer);
			}
			container.Controls.Add(EditContainer);
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		Control _headerContainer = new Control()
			, _footerContainer = new Control()
			, _viewContainer = new Control()
			, _editContainer = new Control();
		#endregion
		#region METHODS
		#endregion
		#endregion

	}
}